package pref.junx.flybird.activity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import butterknife.BindView;
import pref.junx.flybird.R;
import pref.junx.flybird.base.activity.BaseActivity;
import pref.junx.flybird.fragment.SimpleFragment;
import pref.junx.flybird.fragment.daily.Group.GroupFragment;
import pref.junx.flybird.fragment.daily.fri.FriFragment;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {


    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;

    @BindView(R.id.fab)
    FloatingActionButton floatingActionButton;



    @BindView(R.id.viewpager)
    ViewPager viewPager;

    @BindView(R.id.tab_layout)
    TabLayout tabLayout;

    ActionBarDrawerToggle actionBarDrawerToggle;

    private NavigationView navigationView;

    private FragmentPagerAdapter adapter;

    private Snackbar snackbar;

    private String[] tabTitles = {"事件", "群组", "联系人"};

    private int[] imageResId = {R.drawable.ic_timeline_white_18dp, R.drawable.ic_notifications_white_18dp, R.drawable.ic_mail_white_18dp};

    @Override
    protected int getContentView() {

        return R.layout.activity_main;

    }

    @Override
    protected void initWindow() {

        super.initWindow();
        setSupportActionBar(toolbar);
    }

    @Override
    protected void initWidget() {
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        actionBarDrawerToggle.syncState();

        viewPager.setAdapter(adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                switch (position){
                    case 0:return SimpleFragment.newInstance();
                    case 1:return new FriFragment();
                    case 2:return new GroupFragment();
                }
                return null;
            }

            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                Drawable image = getResources().getDrawable(imageResId[position]);
                image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
                SpannableString sb = new SpannableString("   " + tabTitles[position]);
                ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
                sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                return sb;
            }
        });

        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }

    @Override
    protected void initListener() {
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        floatingActionButton.setOnClickListener(this);

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void processClick(View view) {
        switch (view.getId()) {
            case R.id.fab:
                snackbar = Snackbar.make(view, "您目前无法撰写推文", Snackbar.LENGTH_LONG);
                snackbar.setAction("我已了解", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                snackbar.setActionTextColor(Color.WHITE);
                snackbar.getView().setBackgroundColor(getResources().getColor(R.color.colorAccent));
                snackbar.show();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START))
            drawer.closeDrawer(GravityCompat.START);
        else
            super.onBackPressed();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_search) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
                case R.id.timeline:
                    break;
                case R.id.mentions:
                    break;
                case R.id.inbox:
                    break;
                case R.id.activity:
                    break;
                case R.id.profile:
                    break;
                case R.id.lists:
                    break;
                case R.id.search:
                    break;
                case R.id.settings:
                    break;
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

}
